﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Mvc;
using KMF.Common;
using KMF.DAL;
using KMF.WebUI.Areas.KMFadmin.Attributes;

namespace KMF.WebUI.Areas.KMFadmin.Controllers
{
    [AdminLoggedAuthorize]
    public class AdminSynProductController : Controller
    {
        //
        // GET: /Admin/SynProduct/

        public ActionResult Index()
        {
            Session["ListProductSyn"] = null;
            return View();
        }

        public ActionResult ReadExel()
        {
            var model = new List<ProductSynViewDto>();
            if (Session["ListProductSyn"] != null)
                model = Session["ListProductSyn"] as List<ProductSynViewDto>;
            return View(model);
        }
        [HttpPost]
        public ActionResult ReadExel(HttpPostedFileBase filename)
        {
            var model = new List<ProductSynViewDto>();
            if (filename != null && filename.ContentLength > 0)
            {
                var ds = new DataSet(); //du lieu misa
                var PathFolder = Server.MapPath("~/uploads/xml/XmlMisaData/");

                if (!Directory.Exists(PathFolder)) Directory.CreateDirectory(PathFolder);
                else
                {
                    var listfile = Directory.GetFiles(PathFolder);
                    foreach (var item in listfile)
                    {
                        System.IO.File.Delete(item);
                    }
                }

                var ext = Path.GetExtension(PathFolder + filename.FileName);
                if (ext == ".xml")
                {
                    filename.SaveAs(PathFolder + filename.FileName);
                    ds.ReadXml(PathFolder + filename.FileName);
                    var dt = ds.Tables["InventoryItem"];
                    //Lay du lieu hien tai
                    var ListData = ProductDb.GetProductSyn();
                    if (ListData != null && ListData.Count > 0 && dt.Rows.Count > 0)
                    {
                        foreach (var item in ListData)
                        {
                            foreach (DataRow row in dt.Rows)
                            {

                                if (item.Barcode != row["InventoryItemCode"].ToString()) continue;
                                item.PriceNew = item.Price.ToString();
                                item.QuantityNew = item.Quantity.ToString();
                                if (item.Quantity != Convert.ToInt32(row["QuantityInStock"].ToString().Split(',')[0]) && item.Price != Convert.ToDecimal(row["SalePrice"].ToString()))
                                {
                                    item.QuantityNew = row["QuantityInStock"].ToString().Split(',')[0];
                                    item.PriceNew = row["SalePrice"].ToString();
                                    item.chk = "3";//ca sl va gia thay doi
                                    model.Add(item);
                                }
                                else if (item.Quantity != Convert.ToInt32(row["QuantityInStock"].ToString().Split(',')[0]))
                                {
                                    item.QuantityNew = row["QuantityInStock"].ToString().Split(',')[0];
                                    item.chk = "1";//so luong thay doi 
                                    model.Add(item);
                                }
                                else if (item.Price != Convert.ToDecimal(row["SalePrice"].ToString()))
                                {
                                    item.PriceNew = row["SalePrice"].ToString();
                                    item.chk = "2";//gia thay doi 
                                    model.Add(item);
                                }

                            }
                        }
                    }
                }
            }
            Session["ListProductSyn"] = model;
            return View(model);
        }

        public JsonResult UpdateAllProductSyn(string barcode, string Price, int Quanty)
        {
            var model = ProductDb.UpdateProductSyn(barcode, Convert.ToDecimal(Price), Quanty);
            var listModel = Session["ListProductSyn"] as List<ProductSynViewDto>;
            if (listModel != null && listModel.Count>0)
            {
                //loai bo du lieu da dc cap nhap
                foreach (var item in listModel)
                {
                    if (item.Barcode == barcode){
                        listModel.Remove(item);
                        break;
                    }
                }

                Session["ListProductSyn"] = listModel;
            }

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        public JsonResult DestroySession()
        {
            Session["ListProductSyn"] = null;
            return Json(null, JsonRequestBehavior.AllowGet);
        }
    }
}
